package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private EmployeeMapper employeeMapper;

    //查询部门请求
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") EmployeeQueryObject qo){

        PageResult<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult", pageResult);

        List<Employee> employees = employeeService.listALL();
        model.addAttribute("employees", employees);

        model.addAttribute("departments", departmentService.listALL());

        model.addAttribute("user", UserContext.getCurrentUser());
        return "employee/list";
    }
    //处理删除部门请求
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){

        try {

            employeeService.delete(id);
            return new JSONResult();

        } catch(Exception e) {
            e.printStackTrace();

            return new JSONResult(false,"警告");
        }
    }

    //处理新增部门信息
    @RequestMapping("/input")
    @RequiredPermission(name="添加员工",expression = "employee.input")
    public String input(Long id,Model model){

        List<Department> departments = departmentService.listALL();
        model.addAttribute("departments", departments);

        model.addAttribute("roles", roleService.listALL());
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);

        }

        return "employee/input";
    }

    //处理新增和修改页面
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="员工新增或修改",expression = "employee.saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){

        if (employee.getId() != null) {

            employeeService.update(employee,ids);
        }else {
            employeeService.save(employee,ids);

        }
        return "redirect:/employee/list.do";
    }


    //修改密码
    @RequestMapping("/change")
    @ResponseBody
    public JSONResult change(String oldPassword,String newPassword){

        /*if (employee.getId()!=null){

        }*/

        try {
            employeeService.updateAll(oldPassword, newPassword);

            return new JSONResult();
        } catch(Exception e) {
            e.printStackTrace();

            return new JSONResult(false,e.getMessage());
        }


    }

    @RequestMapping("/inputUp")
    public String inputUp(){

        return "/employee/updatePwd";
    }


    @RequestMapping("/grater")
    public String grater(Employee employee,Model model){


        Employee employee1 = employeeService.get(employee.getId());
        model.addAttribute("employee1", employee1);
        return "/employee/resetPwd";
    }

    @RequestMapping("/resetPwd")
    @ResponseBody
    public JSONResult resetPwd(String newPassword,Long id){

        try {

            Employee employee = employeeMapper.selectOne(id);
            employee.setPassword(newPassword);
            employeeService.insert1(employee);
            return new JSONResult();
        } catch(Exception e) {
            e.printStackTrace();

            return new JSONResult(false,e.getMessage());
        }

    }

}
